package com.example.dryyzx.controller;

import com.example.dryyzx.dao.BedMapper;
import com.example.dryyzx.dao.CustomerDietMapper;
import com.example.dryyzx.dao.CustomerMapper;
import com.example.dryyzx.encapsulation.FoodDistributePageEncapsulation;
import com.example.dryyzx.encapsulation.FoodDistributePageEncapsulation2;
import com.example.dryyzx.entity.Bed;
import com.example.dryyzx.entity.Customer;
import com.example.dryyzx.entity.CustomerDiet;
import com.example.dryyzx.entity.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class CustomerDietController {
    @Autowired
    private CustomerDietMapper customerDietMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private BedMapper bedMapper;

    @RequestMapping(value = "food_distribute_page/change_loving", method = RequestMethod.POST)
    public @ResponseBody
    String changeDiet(int id, String loving, String disease, String suggest) {
        CustomerDiet customerDiet = new CustomerDiet();
        customerDiet.setCustomerId(id);
        customerDiet.setLoving(loving);
        customerDiet.setDisease(disease);
        customerDiet.setSuggest(suggest);

        CustomerDiet currentCustomerDiet = customerDietMapper.selectByPrimaryKey(id);
        if(currentCustomerDiet==null){
             int insert = customerDietMapper.insertSelective(customerDiet);
             if(insert == 0)
                 return new JsonResult(false, "添加信息失败").toString();
             else
                 return new JsonResult(true,insert).toString();
        }
        else{
            int update = customerDietMapper.updateByPrimaryKeySelective(customerDiet);
            if(update == 0)
                return new JsonResult(false, "添加信息失败").toString();
            else
                return new JsonResult(true,update).toString();
        }
    }

    public @ResponseBody
    List<CustomerDiet> search_diet_by_customer(List<Customer> customer) {
        List<CustomerDiet> customerDiet = new ArrayList<CustomerDiet>();
        CustomerDiet customerDiettmp = null;
        for (int i = 0; i < customer.size(); i++) {
            customerDiettmp = customerDietMapper.selectByPrimaryKey(customer.get(i).getId());
            customerDiet.add(i, customerDiettmp);
        }
        return customerDiet;
    }

    public @ResponseBody
    List<Customer> search_customer_by_bed(List<Bed> bed) {
        List<Customer> customer = new ArrayList<Customer>();
        Customer customertmp = null;
        for (int i = 0; i < bed.size(); i++) {
            customertmp = customerMapper.selectByBedId(bed.get(i).getId());
            customer.add(i, customertmp);
        }
        return customer;
    }

    List<FoodDistributePageEncapsulation> getEncapsulation(List<Customer> customer,List<CustomerDiet> customerDiet,int limit){
        List<FoodDistributePageEncapsulation> encapsulation = new ArrayList<FoodDistributePageEncapsulation>();
        for (int i=0;i<limit;i++) {
            FoodDistributePageEncapsulation tmp = new FoodDistributePageEncapsulation();
            tmp.id = customer.get(i).getId();//老人编号
            tmp.name = customer.get(i).getName();//老人姓名
            tmp.gender = customer.get(i).getSex();//老人性别，限定为"男","女"
            tmp.age = customer.get(i).getAge();//老人年龄
            if(customerDiet.get(i)==null){
                tmp.loving=null;
                tmp.disease=null;
                tmp.suggest=null;
            }
            else{
                tmp.loving=customerDiet.get(i).getLoving();
                tmp.disease=customerDiet.get(i).getDisease();
                tmp.suggest=customerDiet.get(i).getSuggest();
            }
            encapsulation.add(i, tmp);
        }
        return encapsulation;
    }

    @RequestMapping(value = "food_distribute_page/search_customer_by_name", method = RequestMethod.POST)
    public @ResponseBody
    String getDietByName(String search_key, int page) {
        int data_per_page = 10;
        int total_data_no = customerMapper.countByName(search_key);
        if(total_data_no<=0)
            return new JsonResult(false, "未查找到老人").toString();

        int offset=(page-1)*data_per_page;
        int limit;
        if(total_data_no-offset<data_per_page)
            limit=total_data_no-offset;
        else
            limit=data_per_page;
        if(total_data_no<=offset)
            return new JsonResult(false, "页数过大，数据不够").toString();

        List<Customer> customer = customerMapper.selectByLimitOffsetName(limit,offset,search_key);

        List<CustomerDiet> customerDiet = search_diet_by_customer(customer);
        if (customer.size() != customerDiet.size())
            return new JsonResult(false, "未查找到部分老人的饮食状况").toString();

        FoodDistributePageEncapsulation2 total_data=new FoodDistributePageEncapsulation2();
        List<FoodDistributePageEncapsulation> Encapsulation = getEncapsulation(customer,customerDiet,limit);
        total_data.customer_loving_list=Encapsulation;
        total_data.total_page=(total_data_no-1)/data_per_page+1;

        return new JsonResult(true, total_data).toString();
    }

    @RequestMapping(value = "food_distribute_page/search_customer_by_bed_number", method = RequestMethod.POST)
    public @ResponseBody
    String getDietByBedNumber(String search_key, int page) {
        int data_per_page = 10;
        int total_data_no = bedMapper.countByBedNumber(search_key);
        if(total_data_no<=0)
            return new JsonResult(false, "未查找到床位，或床位无人使用").toString();

        int offset=(page-1)*data_per_page;
        int limit;
        if(total_data_no-offset<data_per_page)
            limit=total_data_no-offset;
        else
            limit=data_per_page;
        if(total_data_no<=offset)
            return new JsonResult(false, "页数过大，数据不够").toString();

        List<Bed> bed=bedMapper.selectByBedNumber(search_key);
        if (bed.size() <= 0)
            return new JsonResult(false, "未查找到床位").toString();
        for(int i=0;i<offset;i++)
            bed.remove(0);
        while(bed.size()>limit)
            bed.remove(limit);

        List<Customer> customer = search_customer_by_bed(bed);
        if (customer.size() <= 0)
            return new JsonResult(false, "未查找到老人").toString();

        /*int bedId=bedMapper.selectBedIdByBedNumber(search_key);
        List<Customer> customer = customerMapper.selectByLimitOffsetBedId(limit,offset,bedId);
        if (customer.size() <= 0)
            return new JsonResult(false, "未查找到老人").toString();*/

        List<CustomerDiet> customerDiet = search_diet_by_customer(customer);
        if (customer.size() != customerDiet.size())
            return new JsonResult(false, "未查找到部分老人的饮食状况").toString();

        FoodDistributePageEncapsulation2 total_data=new FoodDistributePageEncapsulation2();
        List<FoodDistributePageEncapsulation> Encapsulation = getEncapsulation(customer,customerDiet,limit);
        total_data.customer_loving_list=Encapsulation;
        total_data.total_page=(total_data_no-1)/data_per_page+1;

        return new JsonResult(true, total_data).toString();
    }

    @RequestMapping(value = "food_distribute_page/search_customer_by_room_number", method = RequestMethod.POST)
    public @ResponseBody
    String getDietByRoomNumber(String search_key, int page) {
        int data_per_page = 10;
        int total_data_no = bedMapper.countByRoomNumber(search_key);
        if(total_data_no<=0)
            return new JsonResult(false, "未查找到房间内的床位，或床位无人使用").toString();

        int offset=(page-1)*data_per_page;
        int limit;
        if(total_data_no-offset<data_per_page)
            limit=total_data_no-offset;
        else
            limit=data_per_page;
        if(total_data_no<=offset)
            return new JsonResult(false, "页数过大，数据不够").toString();

        List<Bed> bed = bedMapper.selectByRoomNumber(search_key);
        if (bed.size() <= 0)
            return new JsonResult(false, "未查找到床位").toString();
        for(int i=0;i<offset;i++)
            bed.remove(0);
        while(bed.size()>limit)
            bed.remove(limit);

        List<Customer> customer = search_customer_by_bed(bed);
        if (customer.size() <= 0)
            return new JsonResult(false, "未查找到老人").toString();

        List<CustomerDiet> customerDiet = search_diet_by_customer(customer);
        if (customer.size() != customerDiet.size())
            return new JsonResult(false, "未查找到部分老人的饮食状况").toString();

        FoodDistributePageEncapsulation2 total_data=new FoodDistributePageEncapsulation2();
        List<FoodDistributePageEncapsulation> Encapsulation = getEncapsulation(customer,customerDiet,limit);
        total_data.customer_loving_list=Encapsulation;
        total_data.total_page=(total_data_no-1)/data_per_page+1;

        return new JsonResult(true, total_data).toString();
    }

    @RequestMapping(value = "food_distribute_page/search_customer_by_floor_number", method = RequestMethod.POST)
    public @ResponseBody
    String getDietByFloor(String search_key, int page) {
        int data_per_page = 10;
        int total_data_no = bedMapper.countByFloor(search_key);
        if(total_data_no<=0)
            return new JsonResult(false, "未查找到该楼层内的床位，或床位无人使用").toString();

        int offset=(page-1)*data_per_page;
        int limit;
        if(total_data_no-offset<data_per_page)
            limit=total_data_no-offset;
        else
            limit=data_per_page;
        if(total_data_no<=offset)
            return new JsonResult(false, "页数过大，数据不够").toString();

        List<Bed> bed = bedMapper.selectByFloor(search_key);
        if (bed.size() <= 0)
            return new JsonResult(false, "未查找到床位").toString();
        for(int i=0;i<offset;i++)
            bed.remove(0);
        while(bed.size()>limit)
            bed.remove(limit);

        List<Customer> customer = search_customer_by_bed(bed);
        if (customer.size() <= 0)
            return new JsonResult(false, "未查找到老人").toString();

        List<CustomerDiet> customerDiet = search_diet_by_customer(customer);
        if (customer.size() != customerDiet.size())
            return new JsonResult(false, "未查找到部分老人的饮食状况").toString();

        FoodDistributePageEncapsulation2 total_data=new FoodDistributePageEncapsulation2();
        List<FoodDistributePageEncapsulation> Encapsulation = getEncapsulation(customer,customerDiet,limit);
        total_data.customer_loving_list=Encapsulation;
        total_data.total_page=(total_data_no-1)/data_per_page+1;

        return new JsonResult(true, total_data).toString();
    }

    @RequestMapping(value = "food_distribute_page/search_customer_by_building_number", method = RequestMethod.POST)
    public @ResponseBody
    String getDietByPosition(String search_key, int page) {
        int data_per_page = 10;
        int total_data_no = bedMapper.countByPosition(search_key);
        if(total_data_no<=0)
            return new JsonResult(false, "未查找到该楼内的床位，或床位无人使用").toString();

        int offset=(page-1)*data_per_page;
        int limit;
        if(total_data_no-offset<data_per_page)
            limit=total_data_no-offset;
        else
            limit=data_per_page;
        if(total_data_no<=offset)
            return new JsonResult(false, "页数过大，数据不够").toString();

        List<Bed> bed = bedMapper.selectByPosition(search_key);
        if (bed.size() <= 0)
            return new JsonResult(false, "未查找到床位").toString();
        for(int i=0;i<offset;i++)
            bed.remove(0);
        while(bed.size()>limit)
            bed.remove(limit);

        List<Customer> customer = search_customer_by_bed(bed);
        if (customer.size() <= 0)
            return new JsonResult(false, "未查找到老人").toString();

        List<CustomerDiet> customerDiet = search_diet_by_customer(customer);
        if (customer.size() != customerDiet.size())
            return new JsonResult(false, "未查找到部分老人的饮食状况").toString();

        FoodDistributePageEncapsulation2 total_data=new FoodDistributePageEncapsulation2();
        List<FoodDistributePageEncapsulation> Encapsulation = getEncapsulation(customer,customerDiet,limit);
        total_data.customer_loving_list=Encapsulation;
        total_data.total_page=(total_data_no-1)/data_per_page+1;

        return new JsonResult(true, total_data).toString();
    }
}
